package com.xxgc.wujiaoservice.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxgc.wujiaoservice.entity.GiftEntity;
import com.xxgc.wujiaoservice.entity.GiftEntityRepository;
import com.xxgc.wujiaoservice.entity.UsersEntity;
import com.xxgc.wujiaoservice.entity.UsersEntityRepository;
import com.xxgc.wujiaoservice.result.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 直播业务
 */
@RestController
@RequestMapping("/live")
@Tag(name = "直播相关")
public class LiveController {

    @Autowired
    private GiftEntityRepository giftEntityRepository;
    @Autowired
    private UsersEntityRepository usersEntityRepository;

    /**
     * 查询礼物接口
     */
    @GetMapping("/selectGift")
    public Result selectGift(){
        List<GiftEntity> list = giftEntityRepository.list();
        return Result.success(list);
    }

    /**
     * 获取当前登录用户的钱包余额
     */
    @GetMapping("/getUserGold")
    public Result getUserGold(){
        //通过请求头的token获取用户id
        int userId = StpUtil.getLoginIdAsInt();
        UsersEntity usersEntity = usersEntityRepository.getById(userId);
        return Result.success(usersEntity.getGold());
    }




    /**
     * 贡献值
     * 礼物金额 * 0.2 + 系统比例（默认1）
     * */
    // 快捷键 ctrl + shift + u
    private static double SYSTEM_PROPORTION = 1.5;

    //赠送礼物
    @PostMapping("/giveGift/{giftId}")
    public Result giveGift(@PathVariable String giftId){
        //获取个人信息
        int userId = StpUtil.getLoginIdAsInt();
        UsersEntity usersEntity = usersEntityRepository.getById(userId);

        //获取礼物信息
        GiftEntity giftEntity = giftEntityRepository.getById(giftId);
        if(usersEntity.getGold() < giftEntity.getMoney()){
            return Result.fail("对不起，您的余额不足");
        }

        //修改刷礼物后的金额
        int newMoney = usersEntity.getGold() - giftEntity.getMoney();
        usersEntity.setGold(newMoney);
        //贡献值
        double contribution = giftEntity.getMoney() * 0.2 * SYSTEM_PROPORTION;
        usersEntity.setContribution((long)contribution + usersEntity.getContribution());

        //经验值
        /**
         * （当前等级 * 当前等级 * 100） * （等级/10）
         */
        //修改值
        usersEntityRepository.updateById(usersEntity);

        return Result.success();
    }

    /***
     * TODO 待完成事项
     * 条件构造器和分页等第三方插件 解决
     * 排行榜 解决
     * 礼物特效
     * 直播画面 47.108.199.299
     * 项目上线
     * 高能弹幕播报
     * 福袋 讲 Quartz
     */

    @GetMapping("/getUserByContribution")
    public Result getUserByContribution(){
        QueryWrapper<UsersEntity> qw = new QueryWrapper<>();
        //条件构造器 获取用户数据通过贡献值倒排 只要前20个
        qw.orderByDesc("contribution");
        //分页查询
        Page<UsersEntity> page = new Page<>(1, 20);
        Page<UsersEntity> usersEntityPage = usersEntityRepository.page(page, qw);

        return Result.success(usersEntityPage);
    }

    /**
     * 通过礼物id查询礼物详细信息
     */
    @GetMapping("/getGiftInfoById/{giftId}")
    public Result getGiftInfoById(@PathVariable String giftId) {
        GiftEntity giftEntity = giftEntityRepository.getById(giftId);
        return Result.success(giftEntity);
    }

}
